package segmentTree;

/**
 * @program: play-structure
 * @author: baichen
 * 力扣303：给定一个整数数组  nums，求出数组从索引 i 到 j  (i ≤ j) 范围内元素的总和，包含 i,  j 两点。
 **/
public class SumRange {
    private SegmentTree<Integer> segmentTree;

    public SumRange(int[] nums) {
        if (nums.length > 0) {
            //int类型数组不能直接转换为Integer类型数组,所以要先创建一个Integer类型数组，再赋值
            Integer[] data = new Integer[nums.length];
            for (int i = 0; i < nums.length; i++) {
                data[i] = nums[i];
            }
            segmentTree = new SegmentTree<>(data, (a, b) -> a + b);
        }
    }

    public int sumRange(int i, int j) {
        if (segmentTree==null){
            throw new IllegalArgumentException("segmentTree is null");
        }
        return segmentTree.query(i, j);
    }
}
